<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="表单名称" prop="formRef">
        <el-input
          v-model="queryParams.formRef"
          placeholder="请输入表单名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="表单状态" prop="formStatus">
        <el-select v-model="queryParams.formStatus" placeholder="请选择表单状态" clearable size="small">
          <el-option
            v-for="dict in formStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
       </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['form:infos:add']"
        >新增</el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAddReport"
          v-hasPermi="['form:infos:add']"
        >报表设计器</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['form:infos:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['form:infos:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['form:infos:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infosList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['form:infos:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row)"
            v-hasPermi="['form:infos:edit']"
          >查看表单</el-button>
          
         <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['form:infos:remove']"
          >删除</el-button> -->
          
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handlecreateTable(scope.row)"
            v-hasPermi="['form:infos:remove']"
          >生成库表</el-button>
          
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleList(scope.row)"
            v-hasPermi="['form:infos:edit']"
          >查看数据</el-button>
          
          
        </template>
      </el-table-column>
      <el-table-column label="title" align="center" prop="formMemo" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="表单名称" align="center" prop="formRef" />
      <el-table-column label="表单模型" align="center" prop="formModel" />
      <el-table-column label="大小" align="center" prop="size" />
      <el-table-column label="标签位置" align="center" prop="labelPosition" />
      <el-table-column label="标签长度" align="center" prop="labelWidth" />
      <el-table-column label="表单规则" align="center" prop="formRules" />
      <el-table-column label="gutter" align="center" prop="gutter" />
      <el-table-column label="是否禁用" align="center" prop="disalbed" />
      <el-table-column label="span" align="center" prop="span" />
      <el-table-column label="表单按钮" align="center" prop="formBtns" />
      <el-table-column label="表单状态" align="center" prop="formStatus" :formatter="formStatusFormat" />
      <el-table-column label="表单类型" align="center" prop="formType" />
      <el-table-column label="关联工作流" align="center" prop="formWorkflow" />
      
     
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改自定义表单对话框 -->
   <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="表单名称" prop="formRef">
          <el-input v-model="form.formRef" placeholder="请输入表单名称" />
        </el-form-item>
        <el-form-item label="表单模型" prop="formModel">
          <el-input v-model="form.formModel" placeholder="请输入表单模型" />
        </el-form-item>
        <el-form-item label="大小" prop="size">
          <el-input v-model="form.size" placeholder="请输入大小" />
        </el-form-item>
        <el-form-item label="标签位置" prop="labelPosition">
          <el-input v-model="form.labelPosition" placeholder="请输入标签位置" />
        </el-form-item>
        <el-form-item label="标签长度" prop="labelWidth">
          <el-input v-model="form.labelWidth" placeholder="请输入标签长度" />
        </el-form-item>
        <el-form-item label="表单规则" prop="formRules">
          <el-input v-model="form.formRules" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否禁用表单" prop="disalbed">
          <el-input v-model="form.disalbed" placeholder="请输入是否禁用表单" />
        </el-form-item>
        <el-form-item label="span" prop="span">
          <el-input v-model="form.span" placeholder="请输入span" />
        </el-form-item>
        <el-form-item label="表单按钮" prop="formBtns">
          <el-input v-model="form.formBtns" placeholder="请输入表单按钮" />
        </el-form-item>
        <el-form-item label="json表单" prop="fields">
          <el-input v-model="form.fields" placeholder="请输入json表单" />
        </el-form-item>
        <el-form-item label="表单类型" prop="formType">
          <el-select v-model="form.formType" placeholder="请选择表单类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="表单关联工作流" prop="formWorkflow">
          <el-input v-model="form.formWorkflow" placeholder="请输入表单关联工作流" />
        </el-form-item>
        <el-form-item label="备注" prop="formMemo">
          <el-input v-model="form.formMemo" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
    
    
    
    <!-- 打开数据列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
     <div>
         <el-table class="tb-edit" highlight-current-row :data="tableData" style="width: 100%">
             <template v-for="(col ,index) in cols">
                 <el-table-column  :prop="col.prop" :label="col.label"></el-table-column>
             </template>
         </el-table>
     </div>
    </el-dialog>
    
    
    
    
  </div>
</template>

<script>
import { listInfos, getInfos, delInfos, addInfos, updateInfos, exportInfos, createTable ,Encrypt,Decrypt} from "@/api/form/infos";
import Cookies from "js-cookie";
import { getToken } from "@/utils/auth";
export default {
  name: "Infos",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 自定义表单表格数据
      infosList: [],
      formStatusOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        formRef: null,
        formModel: null,
        size: null,
        labelPosition: null,
        labelWidth: null,
        formRules: null,
        gutter: null,
        disalbed: null,
        span: null,
        formBtns: null,
        fields: null,
        formStatus: null,
        formType: null,
        formWorkflow: null,
        formMemo: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //用户登录账号
      username: null,
       // cols prop属性值都是作为 tableData的属性
      cols: [
          { label: "节点编号", prop: "node", type: "normal" },
          { label: "名称", prop: "name", type: "normal" },
          { label: "类型", prop: "type", type: "sort" },
          { label: "坐标", prop: "coordinate", type: "normal" }
      ],
      tableData: [
          {
              node: "0051",
              name: " 机库顶",
              type: "UWB",
              status: "正常",
              coordinate: "12.21,34.45,34.6"
          },
          {
              node: "0061",
              name: "机库门",
              type: "GPS",
              status: "低电",
              coordinate: "45.41,67.45,78.6"
          }
      ]
    };
  },
  created() {
    const username = Cookies.get("username");
    this.username=username;
    this.getList();
    
    this.getDicts("FORM_STATUS").then(response => {
         this.formStatusOptions = response.data;
       });
       
  },
  methods: {
    /** 查询自定义表单列表 */
    getList() {
      this.loading = true;
      listInfos(this.queryParams).then(response => {
        this.infosList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
     // 表单状态字典翻译
      formStatusFormat(row, column) {
        return this.selectDictLabel(this.formStatusOptions, row.formStatus);
      },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        formRef: null,
        formModel: null,
        size: null,
        labelPosition: null,
        labelWidth: null,
        formRules: null,
        gutter: null,
        disalbed: null,
        span: null,
        formBtns: null,
        fields: null,
        formStatus: "0",
        formType: null,
        formWorkflow: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        formMemo: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      //表单设计器地址
      window.open("http://qxsdcloud.com/form-generator/#/?uid="+this.username);
      //window.open("http://localhost:8080/#/?uid="+this.username);
     /* this.reset();
      this.open = true;
      this.title = "添加自定义表单"; */
    },
    //进入报表设计器
    handleAddReport(){
      const ids ="addExcelReport";
      /* const ids = row.id ;
       this.reset();
       this.open = true;
       this.title = "数据列表"; */
       let username = Encrypt(this.username,'1234567890123456','1234567890123456')
       //报表地址
       window.open("http://qxsdcloud.com:18083/jmreport/list?token="+ids+"-"+ username);
       //window.open("http://localhost:18083/jmreport/list?token="+ids+"-"+ username );
    },
    /** 查看数据列表操作 */
    handleList(row) {
      const ids ="custom_"+row.id;
     /* const ids = row.id ;
      this.reset();
      this.open = true;
      this.title = "数据列表"; */
     let username = Encrypt(this.username,'1234567890123456','1234567890123456')
      //报表地址
		window.open("http://qxsdcloud.com:18083/jmreport/index/"+ids+"?token="+ids+"-"+ username);
      // window.open("http://localhost:18083/jmreport/index/"+ids+"?token="+ids+"-"+ username);
      
    },
    //查看渲染的页面
    handleView(row) {
      const ids = row.id ;
      window.open("http://qxsdcloud.com/form-generator/#/form?id="+ids+"&uid="+this.username);
     // window.open("http://localhost:8080/#/form?id="+ids+"&uid="+this.username);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfos(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改自定义表单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfos(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addInfos(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除自定义表单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfos(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    handlecreateTable(row) {
      const ids = row.id || this.ids;
      debugger
      this.$confirm('是否确认生成自定义表单编号为"' + ids + '"的物理库表?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return createTable(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("生成成功，可以开始实用该表单了!");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有自定义表单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportInfos(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>